//
//  SLStatusCell.swift
//  Sina15
//
//  Created by 盼 on 16/4/13.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SnapKit

class SLStatusCell: UITableViewCell {
    
    //纪录工具条顶部的约束
    var toolBarCons: Constraint?
    
    var statusModel: SLStatusViewModel? {
        didSet {
//            print(statusModel?.status?.pic_urls?.count)
//            print(statusModel?.status?.retweeted_status?.pic_urls?.count)
//            print("-------")
            originalView.statusModel = statusModel
            toolBar.statusModel = statusModel
            
            //修改约束之前先卸载约束
            self.toolBarCons?.uninstall()
            
            if statusModel?.status?.retweeted_status != nil {
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                   self.toolBarCons = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
                //设置隐藏
                retweetedView.hidden = false
               
                //设置数据源
                retweetedView.statusModel = statusModel
            }else{
                
                //修改约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                //转发微博视图隐藏
                retweetedView.hidden = true
                
                //设置数据源
                retweetedView.statusModel = nil
            }
        }
    }
    
    //重写构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置cell选中的背景色
        selectionStyle = .None
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    func setupUI(){
        
        //添加到cell中
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(toolBar)
        
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
        }
        
        //转发微博的约束
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo( contentView)
            make.top.equalTo(originalView.snp_bottom)
//            make.height.equalTo(100)
        }
        
        //设置工具条的约束
        toolBar.snp_makeConstraints { (make) -> Void in
          self.toolBarCons = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_bottom)
        }
    }
    
    //懒加载原创微博视图
    private lazy var originalView: SLOriginalView = {
       let v = SLOriginalView()
        v.backgroundColor = UIColor.whiteColor()
        return v
    }()
    
    //转发微博视图
    private lazy var retweetedView: SLStatusRetweetedView = SLStatusRetweetedView()
    
    //底部工具条
    private lazy var toolBar: SLStatusToolBar = SLStatusToolBar()
}
